package kr.co.bettersoftportal.site.management.service;



import kr.co.bettersoftportal.site.domain.NoticeBoard;
import kr.co.bettersoftportal.site.domain.UploadFile;
import kr.co.bettersoftportal.site.management.dao.MemberDao;
import kr.co.bettersoftportal.site.management.dao.NoticeBoardDao;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.util.List;
import java.util.UUID;


public class NoticeBoardServiceImpl implements NoticeBoardService{

    @Autowired
    private MemberDao memberDao = null;

    @Autowired
    private NoticeBoardDao noticeBoardDao = null;

    public List getBoardListBySearchword(NoticeBoard noticeBoard) {
        return noticeBoardDao.getBoardListBySearchword(noticeBoard);
    }

    public List getBoardListByNo(NoticeBoard noticeBoard){
        return noticeBoardDao.getBoardListByNo(noticeBoard);
    }

    public String getTotalCountBySearchword(NoticeBoard noticeBoard) {
        return noticeBoardDao.getTotalCountBySearchword(noticeBoard);
    }

    public String getTotalCountByNo(NoticeBoard noticeBoard) {
        return noticeBoardDao.getTotalCountBySearchword(noticeBoard);
    }

    public String writeBoard(NoticeBoard board){
        return noticeBoardDao.writeBoard(board);
    }

    public String updateBoard(NoticeBoard board){
        return noticeBoardDao.updateBoard(board);
    }

    public String deleteBoard(NoticeBoard board){
        return noticeBoardDao.deleteBoard(board);
    }

    public NoticeBoard getBoardByNo(String notice_board_no){
        return noticeBoardDao.getBoardByNo(notice_board_no);
    }

    public String updateReplyCount(NoticeBoard board){
        return noticeBoardDao.updateReplyCount(board);
    }

    public String updateHitCount(NoticeBoard board){
        return noticeBoardDao.updateHitCount(board);
    }

    public String updateVoteCount(NoticeBoard board){
        return noticeBoardDao.updateVoteCount(board);
    }

    public String updateNegativeCount(NoticeBoard board){
        return noticeBoardDao.updateNegativeCount(board);
    }

    public String updateReportCount(NoticeBoard board){
        return noticeBoardDao.updateReportCount(board);
    }

    /**
     * 파일 업로드 서비스
     * @param file
     * @param realPath
     * @return
     */
    public UploadFile uploadFile(MultipartFile file, String realPath){

        InputStream stream; // Input Stream 생성

        UUID uuid = UUID.randomUUID(); // 랜덤 UUID 이름 생성
        String tempFileName = uuid.toString(); // 임시 파일 이름 저장

        try {
            stream = file.getInputStream();

            OutputStream bos = new FileOutputStream(realPath + tempFileName);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            stream.close();

            // 파일 쓰고 stream 닫기

            /*
            if (logger.isDebugEnabled()) {
                logger.debug("The file has been written to \"" + realPath
                        + tempFileName);
            }
            */

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        UploadFile boardFile = new UploadFile();
        boardFile.setFileName(file.getOriginalFilename());
        boardFile.setFileSize(Long.toString(file.getSize()));
        boardFile.setContentType(file.getContentType());
        boardFile.setTempFileName(tempFileName);

        return boardFile;
    }

    public String updateBoardNotImage(NoticeBoard board) {
        return noticeBoardDao.updateBoardNotImage(board);
    }


    ///////////////////////////////////////
	// Spring Framework Setter Injection //
	///////////////////////////////////////
    

}
